CREATE OR REPLACE FUNCTION WSO2_TOKEN_CLEANUP_DATA_RESTORATION_SP() RETURNS void AS $$
DECLARE


rowcount bigint;
enableLog boolean;
logLevel VARCHAR(10);

BEGIN

-- ------------------------------------------
-- CONFIGURABLE ATTRIBUTES
-- ------------------------------------------
enableLog := TRUE; -- ENABLE LOGGING [DEFAULT : TRUE]
logLevel := 'TRACE'; -- SET LOG LEVELS : TRACE



IF (enableLog) THEN
RAISE NOTICE 'WSO2_TOKEN_CLEANUP_DATA_RESTORATION_SP STARTED .... !';
RAISE NOTICE '';
END IF;


-- ---------------------

SELECT COUNT(1) INTO rowcount  FROM PG_CATALOG.PG_TABLES WHERE SCHEMANAME = CURRENT_SCHEMA() AND TABLENAME IN ('idn_oauth2_access_token');
IF (rowcount = 1)
THEN
IF (enableLog AND logLevel IN ('TRACE')) THEN
RAISE NOTICE 'CLEANUP DATA RESTORATION STARTED ON IDN_OAUTH2_ACCESS_TOKEN TABLE !';
END IF;
INSERT INTO IDN_OAUTH2_ACCESS_TOKEN SELECT A.* FROM BAK_IDN_OAUTH2_ACCESS_TOKEN A LEFT JOIN IDN_OAUTH2_ACCESS_TOKEN B ON A.TOKEN_ID = B.TOKEN_ID WHERE B.TOKEN_ID IS NULL;
GET DIAGNOSTICS rowcount := ROW_COUNT;
IF (enableLog ) THEN
RAISE NOTICE 'CLEANUP DATA RESTORATION COMPLETED ON IDN_OAUTH2_ACCESS_TOKEN WITH %',ROWCOUNT;
END IF;
END IF;

-- ---------------------

SELECT COUNT(1) INTO rowcount  FROM PG_CATALOG.PG_TABLES WHERE SCHEMANAME = CURRENT_SCHEMA() AND TABLENAME IN ('idn_oauth2_authorization_code');
IF (rowcount = 1)
THEN
IF (enableLog AND logLevel IN ('TRACE')) THEN
RAISE NOTICE 'CLEANUP DATA RESTORATION STARTED ON IDN_OAUTH2_AUTHORIZATION_CODE TABLE !';
END IF;
INSERT INTO IDN_OAUTH2_AUTHORIZATION_CODE SELECT A.* FROM BAK_IDN_OAUTH2_AUTHORIZATION_CODE A LEFT JOIN IDN_OAUTH2_AUTHORIZATION_CODE B ON A.CODE_ID = B.CODE_ID WHERE B.CODE_ID IS NULL;
GET DIAGNOSTICS rowcount := ROW_COUNT;
IF (enableLog ) THEN
RAISE NOTICE 'CLEANUP DATA RESTORATION COMPLETED ON IDN_OAUTH2_AUTHORIZATION_CODE WITH %',ROWCOUNT;
END IF;
END IF;

-- ---------------------

SELECT COUNT(1) INTO rowcount  FROM PG_CATALOG.PG_TABLES WHERE SCHEMANAME = CURRENT_SCHEMA() AND TABLENAME IN ('idn_oauth2_access_token_scope');
IF (rowcount = 1)
THEN
IF (enableLog AND logLevel IN ('TRACE')) THEN
RAISE NOTICE 'CLEANUP DATA RESTORATION STARTED ON IDN_OAUTH2_ACCESS_TOKEN_SCOPE TABLE !';
END IF;
INSERT INTO IDN_OAUTH2_ACCESS_TOKEN_SCOPE SELECT A.* FROM BAK_IDN_OAUTH2_ACCESS_TOKEN_SCOPE A LEFT JOIN IDN_OAUTH2_ACCESS_TOKEN_SCOPE B ON A.TOKEN_ID = B.TOKEN_ID AND A.TOKEN_SCOPE = B.TOKEN_SCOPE WHERE B.TOKEN_ID IS NULL;
GET DIAGNOSTICS rowcount := ROW_COUNT;
IF (enableLog ) THEN
RAISE NOTICE 'CLEANUP DATA RESTORATION COMPLETED ON IDN_OAUTH2_ACCESS_TOKEN_SCOPE WITH %',ROWCOUNT;
END IF;
END IF;

-- ---------------------

SELECT COUNT(1) INTO rowcount  FROM PG_CATALOG.PG_TABLES WHERE SCHEMANAME = CURRENT_SCHEMA() AND TABLENAME IN ('idn_oidc_req_object_reference');
IF (rowcount = 1)
THEN
IF (enableLog AND logLevel IN ('TRACE')) THEN
RAISE NOTICE 'CLEANUP DATA RESTORATION STARTED ON IDN_OIDC_REQ_OBJECT_REFERENCE TABLE !';
END IF;
INSERT INTO IDN_OIDC_REQ_OBJECT_REFERENCE SELECT A.* FROM BAK_IDN_OIDC_REQ_OBJECT_REFERENCE A LEFT JOIN IDN_OIDC_REQ_OBJECT_REFERENCE B ON A.ID = B.ID WHERE B.ID IS NULL;
GET DIAGNOSTICS rowcount := ROW_COUNT;
IF (enableLog ) THEN
RAISE NOTICE 'CLEANUP DATA RESTORATION COMPLETED ON IDN_OIDC_REQ_OBJECT_REFERENCE WITH %',ROWCOUNT;
END IF;
END IF;

-- --------------------

SELECT COUNT(1) INTO rowcount  FROM PG_CATALOG.PG_TABLES WHERE SCHEMANAME = CURRENT_SCHEMA() AND TABLENAME IN ('idn_oidc_req_object_claims');
IF (rowcount = 1)
THEN
IF (enableLog AND logLevel IN ('TRACE')) THEN
RAISE NOTICE 'CLEANUP DATA RESTORATION STARTED ON IDN_OIDC_REQ_OBJECT_CLAIMS TABLE !';
END IF;
INSERT INTO IDN_OIDC_REQ_OBJECT_CLAIMS SELECT A.* FROM BAK_IDN_OIDC_REQ_OBJECT_CLAIMS A LEFT JOIN IDN_OIDC_REQ_OBJECT_CLAIMS B ON A.ID = B.ID WHERE B.ID IS NULL;
GET DIAGNOSTICS rowcount := ROW_COUNT;
IF (enableLog ) THEN
RAISE NOTICE 'CLEANUP DATA RESTORATION COMPLETED ON IDN_OIDC_REQ_OBJECT_CLAIMS WITH %',ROWCOUNT;
END IF;
END IF;

-- -------------------

SELECT COUNT(1) INTO rowcount  FROM PG_CATALOG.PG_TABLES WHERE SCHEMANAME = CURRENT_SCHEMA() AND TABLENAME IN ('idn_oidc_req_obj_claim_values');
IF (rowcount = 1)
THEN
IF (enableLog AND logLevel IN ('TRACE')) THEN
RAISE NOTICE 'CLEANUP DATA RESTORATION STARTED ON IDN_OIDC_REQ_OBJ_CLAIM_VALUES TABLE !';
END IF;
INSERT INTO IDN_OIDC_REQ_OBJ_CLAIM_VALUES SELECT A.* FROM BAK_IDN_OIDC_REQ_OBJ_CLAIM_VALUES A LEFT JOIN IDN_OIDC_REQ_OBJ_CLAIM_VALUES B ON A.ID = B.ID WHERE B.ID IS NULL;
GET DIAGNOSTICS rowcount := ROW_COUNT;
IF (enableLog ) THEN
RAISE NOTICE 'CLEANUP DATA RESTORATION COMPLETED ON IDN_OIDC_REQ_OBJ_CLAIM_VALUES WITH %',ROWCOUNT;
END IF;
END IF;


IF (enableLog) THEN
RAISE NOTICE 'WSO2_TOKEN_CLEANUP_DATA_RESTORATION_SP COMPLETED .... !';
RAISE NOTICE '';
END IF;




END;
$$
LANGUAGE 'plpgsql';
